package com.woniuxy.service.impl;

import com.woniuxy.dao.AgentLevelSettingDao;
import com.woniuxy.dao.ManagerDao;
import com.woniuxy.entity.Levelapplicationform;
import com.woniuxy.entity.Member;
import com.woniuxy.entity.User;
import com.woniuxy.service.AgentLevelSettingService;
import com.woniuxy.service.AgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Auther: Administrator  <br/>
 * Date: 2021/2/24:10:20  <br/>
 * Description:
 */
@Service
public class AgentLevelSettingServiceImpl implements AgentLevelSettingService {

    @Autowired(required = false)
    private AgentLevelSettingDao agentLevelSettingDao;
    @Autowired(required = false)
    private ManagerDao managerDao;
    //当前用户提交申请
    @Override
    public int insertLevelApplicationform(Levelapplicationform levelapplicationform,HttpSession session) {
        //从session获取当前登录用户信息
        Member member = (Member) session.getAttribute("member");
        //获取当前登录用户的当前等级
        levelapplicationform.setNowLevel(member.getLevelid());
        //设置该申请状态为待处理
        levelapplicationform.setStatus(0);
        //设置当前登录用户的memberId
        levelapplicationform.setMemberId(member.getId());
        //设置当前用户现在的上级代理
        levelapplicationform.setReferenceId(member.getReference());
        //设置用户当前的上级代理的上级代理
        //通过上级代理id查询上上级代理id
        Member newMember=managerDao.selectOne(member.getReference());
        //设置上上级代理id
        levelapplicationform.setNewreferenceId(newMember.getReference());
        //设置当前系统时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        levelapplicationform.setApplicationTime(df.toString());
        int row=agentLevelSettingDao.insertLevelApplicationform(levelapplicationform);
        return row;
    }
    //查询当前账号提交的所有申请
    @Override
    public List<Levelapplicationform> selectMyLevelApplicationform(HttpSession session) {
        //测试时将数据写死，测试完成后将下列取消注释并修改对应参数
//        User user = (User)session.getAttribute("account");
        List<Levelapplicationform> levelapplicationforms = agentLevelSettingDao.selectMyLevelApplicationform(1);
        return levelapplicationforms;
    }
    //查询当前账号收到的申请
    @Override
    public List<Levelapplicationform> selectOtherLevelApplicationform(HttpSession session) {
        User user = (User)session.getAttribute("account");
        List<Levelapplicationform> levelapplicationforms = agentLevelSettingDao.selectOtherLevelApplicationform(user.getMemberid());
        return levelapplicationforms;
    }
    //删除提交的申请
    @Override
    public int deleteLevelApplicationform(int id) {
        int row=agentLevelSettingDao.deleteLevelApplicationform(id);
        return row;
    }
    //修改自己提交的申请
    @Override
    public int updateLevelApplicationform(Levelapplicationform levelapplicationform) {
        int row=agentLevelSettingDao.updateLevelApplicationform(levelapplicationform);
        return row;
    }
}
